package com.example.test.启动线程.CAS;

public class Problem {

    public static int a = 0;

    public static void main(String[] args) {


        Thread thread = new Thread() {
            @Override
            public void run() {
                int old = a;
                System.out.println("内存值:" + a);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean cas = cas(a, old, a + 1);
                System.out.println("设置结果:" + cas);
                System.out.println("输出值:" + a);
            }
        };

        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a = 5;
//        a = 0;
    }

    public static boolean cas(int neicun, int yuqi, int ne) {
        if (neicun == yuqi) {
            a = ne;
            return true;
        }
        return false;
    }
}
